//
// Created by 66477 on 2022/9/28.
//

#ifndef DATA_STRUCTURE_LIST4_1_H
#define DATA_STRUCTURE_LIST4_1_H

#include "Node4_1.h"
#include <cstring>

class List4_1 {
private:
    Node4_1 *head = nullptr;
    Node4_1 *p = nullptr;
    Node4_1 *half = nullptr;
public:
    List4_1(string s) {
        Node4_1 *x = nullptr;
        int l = s.length();
        for (int i = 0; i < l; ++i) {
            x = add(s[i]);
            if (i == l / 2) {
                if (s.length() % 2 == 0) {
                    half = new Node4_1('2');
                    half->setNext(x);
                } else {
                    half = new Node4_1('1');
                    half->setNext(x);
                }
            }
        }
        p = head;
    }

    Node4_1 *add(char c) {
        Node4_1 *p = new Node4_1(c), *q = head;
        if (head == nullptr) {
            head = p;
        } else {
            while (q->getNext() != nullptr) {
                q = q->getNext();
            }
            q->setNext(p);
        }
        return p;
    }

    List4_1 *print() {
        cout << "Q1 List:";
        if (head != nullptr) {
            Node4_1 *q = head;
            while (q != nullptr) {
                cout << q->getData() << " ";
                q = q->getNext();
            }
        }
        cout << endl;
        return this;
    }

    Node4_1 *get() {
        if (p == nullptr) {
            p = head;
            return nullptr;
        } else {
            Node4_1 *c = p;
            p = p->getNext();
            return c;
        }

    }

    Node4_1 *getHalf() const {
        return half;
    }
};

#endif //DATA_STRUCTURE_LIST4_1_H
